﻿/**********************************************************************
/// <summary>
// Name of File     : DBParseUtils.cs
// Author               : Sukhminder
// Create date      : 8th May 2012
/// </summary>
************************************************************************/
using System;
using System.Data;

namespace Crystal.CLL
{
    public class DBParseUtils
    {
        private DBParseUtils()
        { }

        /// <summary>
        /// Return the string after pasring it
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static string GetFieldValueString(DataRow dr, string fieldValue)
        {
            object obVal = dr[fieldValue];
            if (Convert.IsDBNull(obVal))
            {
                return String.Empty;
            }
            return obVal.ToString();
        }

        /// <summary>
        /// Return the boolean after pasring it- Added my Jitender
        /// </summary>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static bool GetFieldValueBool(bool fieldValue)
        {
            bool? value = fieldValue;
            if (!value.HasValue) //check for a value
            {
                value = false;
            }
            return Convert.ToBoolean(value);
        }

        /// <summary>
        /// Return the string after pasring it- Added my Jitender 
        /// </summary>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static string GetFieldValueString(string fieldValue)
        {
            if (string.IsNullOrEmpty(fieldValue))
            {
                return string.Empty;
            }
            return fieldValue;
        }

        /// <summary>
        ///  Parse the date in case of null- Added my Jitender
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        /// 
        public static DateTime GetFieldValueDateTime(DateTime fieldValue)
        {
            object obVal = fieldValue;
            if (Convert.IsDBNull(obVal))
            {
                return DateTime.MinValue;
            }
            return (DateTime)obVal;
        }

        /// <summary>
        /// Return the Int32 after pasring it
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static Int32 GetFieldValueInt32(DataRow dr, string fieldValue)
        {
            object obVal = dr[fieldValue];
            if (Convert.IsDBNull(obVal))
            {
                return 0;
            }
            return Convert.ToInt32(obVal);
        }

        /// <summary>
        /// Parse the int in case of null- Added my Jitender 
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static Int32 GetFieldValueInt32(int value)
        {
            int? fieldValue = value;
            if (!fieldValue.HasValue)
            {
                fieldValue = 0;
            }
            return Convert.ToInt32(fieldValue);
        }

        public static Int64 GetFieldValueInt64(Int64 value)
        {
            Int64? fieldValue = value;
            if (!fieldValue.HasValue)
            {
                fieldValue = 0;
            }
            return Convert.ToInt64(fieldValue);
        }


        /// <summary>
        /// Parse the int16 in case of null
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static Int16 GetFieldValueInt16(Int16 value)
        {
            Int16? fieldValue = value;
            if (!fieldValue.HasValue)
            {
                fieldValue = 0;
            }
            return Convert.ToInt16(fieldValue);
        }

        ///// <summary>
        ///// Parse the int16 in case of null
        ///// </summary>
        ///// <param name="dr"></param>
        ///// <param name="fieldValue"></param>
        ///// <returns></returns>
        //public static Int16 GetFieldValueInt16(DataRow dr, string fieldValue)
        //{
        //    object obVal = dr[fieldValue];
        //    if (Convert.IsDBNull(obVal))
        //    {
        //        return 0;
        //    }
        //    return Convert.ToInt16(obVal);
        //}

        /// <summary>
        /// Parse the byte in case of null
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static Int16 GetFieldValueByte(byte value)
        {
            byte? fieldValue = value;
            if (!fieldValue.HasValue)
            {
                fieldValue = 0;
            }
            return Convert.ToByte(fieldValue);
        }

        /// <summary>
        /// Parse the Int64 in case of null
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static Int64 GetFieldValueInt64(DataRow dr, string fieldValue)
        {
            object obVal = dr[fieldValue];
            if (Convert.IsDBNull(obVal))
            {
                return 0;
            }
            return Convert.ToInt64(obVal);
        }

        /// <summary>
        /// Parse the Bool in case of null
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static bool GetFieldValueBit(DataRow dr, string fieldValue)
        {
            object obVal = dr[fieldValue];
            if (Convert.IsDBNull(obVal))
            {
                return false;
            }
            return Convert.ToBoolean(obVal);
        }

        /// <summary>
        /// Get Field Value Decimal
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static decimal GetFieldValueDecimal(DataRow dr, string fieldValue)
        {
            object obVal = dr[fieldValue];
            if (Convert.IsDBNull(obVal))
            {
                return 0.0M;
            }
            return Convert.ToDecimal(obVal);
        }

        /// <summary>
        /// Return the decimal after pasring it- Added by SPR
        /// </summary>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static decimal GetFieldValueDecimal(decimal fieldValue)
        {
            decimal? value = fieldValue;
            if (!value.HasValue) //check for a value
            {
                value = 0.0M;
            }
            return Convert.ToDecimal(fieldValue);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="record"></param>
        /// <param name="columnIndex"></param>
        /// <returns></returns>
        public static T? GetValue<T>(object record)
        where T : struct
        {
            if (Convert.IsDBNull(record))
                return null;
            else
                return(T)Convert.ChangeType(record, typeof(T));
        }
        public static string GetValueString(object record)        
        {
            if (Convert.IsDBNull(record))
                return string.Empty;
            else
                return (string)Convert.ChangeType(record, typeof(string));
        }
        /// <summary>
        /// Get Field Value Guid
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static Guid GetFieldValueGuid(DataRow dr, string fieldValue)
        {
            object obVal = dr[fieldValue];
            if (Convert.IsDBNull(obVal))
            {
                return Guid.Empty;
            }
            return new Guid(obVal.ToString());
        }

        /// <summary>
        /// Parse the DateTime in case of null
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public static DateTime GetFieldValueDateTime(DataRow dr, string fieldValue)
        {
            object obVal = dr[fieldValue];
            if (Convert.IsDBNull(obVal))
            {
                return DateTime.MinValue;
            }
            return (DateTime)obVal;
        }
    }
}
